%%% Copyright 2010 Google Inc.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.

%%% @copyright 2010 Google Inc. All Rights Reserved.
%%% @author <jacoby@google.com> Full Name [http://sites/madcloud]
%%% @version {@version}
%%% @doc
%%% Tests for the XML/JSON output module {@link xj}.
%%% @see xj
%%%
%%% @todo(jacoby) Add tests to cover edge cases and escaping.

-module(xj_tests).

-include_lib("eunit/include/eunit.hrl").

%%%----------------------------------------------------------------------------
%%% JSON tests
%%%----------------------------------------------------------------------------

%% @doc Trivial json test.
trivial_json_test() ->
  Struct = trivial(),
  String = "{\"person\":true}",
  ?assertEqual(String, lists:flatten(xj:struct_to_json(Struct))).

%% @doc Simple json test.
simple_json_test() ->
  Struct = simple(),
  String = "{\"person\":{\"mood\":\"happy\",\"pulse\":76,\"name\":\"Mr Happy\"}}",
  ?assertEqual(String, lists:flatten(xj:struct_to_json(Struct))).

%% @doc Complex json test.
complex_json_test() ->
  Struct = complex(),
  String =  "{\"person\":{\"mood\":\"happy\",\"pulse\":76,\"name\":\"Mr Happy\"},"
              "\"contents\":["
                "{\"day\":{\"name\":\"Mon\",\"in\":\"09:31\",\"hours\":7}},"
                "{\"day\":{\"name\":\"Tue\",\"in\":\"09:22\",\"hours\":8},"
                 "\"contents\":[{\"carparked\":true}]}"
            "]}",
  ?assertEqual(String, lists:flatten(xj:struct_to_json(Struct))).

%%%----------------------------------------------------------------------------
%%% XML tests
%%%----------------------------------------------------------------------------

%% @doc Trivial XML test.
trivial_xml_test() ->
  Struct = trivial(),
  String = "<person/>",
  ?assertEqual(String, lists:flatten(xj:struct_to_xml(Struct))).

%% @doc Simple XML test.
simple_xml_test() ->
  Struct = simple(),
  String = "<person mood='happy' pulse='76' name='Mr Happy'/>",
  ?assertEqual(String, lists:flatten(xj:struct_to_xml(Struct))).

%% @doc Complex XML test.
complex_xml_test() ->
  Struct = complex(),
  String = "<person mood='happy' pulse='76' name='Mr Happy'>"
             "<day name='Mon' in='09:31' hours='7'/>"
             "<day name='Tue' in='09:22' hours='8'>"
               "<carparked/>"
             "</day>"
           "</person>",
  ?assertEqual(String, lists:flatten(xj:struct_to_xml(Struct))).

%%%----------------------------------------------------------------------------
%%% Test structs
%%%----------------------------------------------------------------------------

trivial() ->
  % Element only.
  {person}.

simple() ->
  % Element and attributes of different types.
  {person, [{mood, happy}, {pulse, 76}, {name, "Mr Happy"}]}.

complex() ->
  % Element, attributes and nested content list.
  {person, [{mood, happy}, {pulse, 76}, {name, "Mr Happy"}],
    % Mixed content list
    [
      {day, [{name, 'Mon'}, {in, "09:31"}, {hours, 7}]},
      {day, [{name, 'Tue'}, {in, "09:22"}, {hours, 8}],
        [
          {carparked}
        ]
      }
    ]
  }.
